package cn.com.egova.demoapplication.view;

import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.TimeInterpolator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import cn.com.egova.demoapplication.bean.Point;
import cn.com.egova.demoapplication.tools.PointSinEvaluator;

/**
 * Created by yuanchao on 2017/3/8.
 */

public class PointAnimView extends View {
    public static final String TAG = "PointAnimView";
    public static final int RADIUS = 80;
    private Paint circlePaint;
    private Paint linePaint;
    private Point point; //当前位置的坐标.
    private int color;  //圆圈的当前颜色.
    private int radius = RADIUS;
    private AnimatorSet animSet;
    private TimeInterpolator interpolator = new LinearInterpolator();

    public PointAnimView(Context context) {
        this(context, null);
    }

    public PointAnimView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PointAnimView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.TRANSPARENT);  //初始时无颜色.
        circlePaint.setStyle(Paint.Style.FILL);

        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setColor(Color.BLACK);
        linePaint.setStrokeWidth(3);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        //绘制横竖2条固定的直线
        drawLine(canvas);

        if (point == null) {
            initAnimator();
        } else {
            drawCircle(canvas);
        }
    }


    /**
     * 画直线
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {
        canvas.drawLine(10, getHeight() / 2, getWidth(), getHeight() / 2, linePaint);
        canvas.drawLine(10, getHeight() / 2 - 150, 10, getHeight() / 2 + 150, linePaint);
    }

    /**
     * 画圆形
     *
     * @param canvas
     */
    private void drawCircle(Canvas canvas) {
        //显然PointAnimView的3个属性都是变化的: point,color,radius.
        circlePaint.setColor(color);
        canvas.drawCircle(point.getX(), point.getY(), radius, circlePaint);
    }

    /**
     * 给本控件初始化动画.
     */
    private void initAnimator() {
        point = new Point(RADIUS, RADIUS);
        Point startPoint = new Point(RADIUS, RADIUS);
        Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);

        //坐标变化
        ValueAnimator trackAnimator = ValueAnimator.ofObject(new PointSinEvaluator(), startPoint, endPoint);
        trackAnimator.setRepeatCount(-1);   //等价于:Animation.INFINITE  表示无限循环.
        trackAnimator.setRepeatMode(ValueAnimator.REVERSE);   //动画结束后反向开始.
        trackAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                point = (Point) animation.getAnimatedValue();
                postInvalidate();
            }
        });

        //颜色变化
        ObjectAnimator colorAnimator = ObjectAnimator.ofObject(this, "color", new ArgbEvaluator(), Color.YELLOW, Color.BLUE, Color.WHITE, Color.RED);
        colorAnimator.setRepeatCount(-1);
        colorAnimator.setRepeatMode(ValueAnimator.REVERSE);
        colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                color = (int) animation.getAnimatedValue();
            }
        });

        //大小变化.
        ValueAnimator scaleAnimator = ValueAnimator.ofInt(20, 80, 60, 10, 35, 55, 10);
        scaleAnimator.setRepeatCount(-1);
        scaleAnimator.setRepeatMode(ValueAnimator.REVERSE);
        scaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                radius = (int) animation.getAnimatedValue();
            }
        });

        //所有动画同时执行.
        animSet = new AnimatorSet();
        animSet.play(trackAnimator).with(colorAnimator).with(scaleAnimator);
        animSet.setDuration(5000);
        animSet.setInterpolator(interpolator);
//        animSet.start();
    }



    public void setInterpolator(int type ) {
        switch (type) {
            case 1:
                interpolator = new DecelerateInterpolator();
                break;
            case 2:
                interpolator = new AccelerateInterpolator();
                break;
            case 3:
                interpolator = new AccelerateDecelerateInterpolator();
                break;
            case 4:
                interpolator = new AnticipateInterpolator();
                break;
            case 5:
                interpolator = new AnticipateOvershootInterpolator();
                break;
            case 6:
                interpolator = new BounceInterpolator();
            case 7:
                interpolator = new CycleInterpolator(3);
            case 8:
                interpolator = new LinearInterpolator();
            default:
                interpolator = new LinearInterpolator();
                break;
        }
    }

    public TimeInterpolator getInterpolator(){
        return interpolator;
    }


    public void startAnimation(){
        if(animSet!=null){
            animSet.start();
        }
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)     //对应API:19.
    public void pauseAnimation() {
        if (animSet != null) {
            animSet.pause();
        }
    }


    public void stopAnimation() {
        if (animSet != null) {
            animSet.cancel();
            this.clearAnimation();
        }
    }

}
